define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/treatment/index' + location.search,
                    add_url: 'order/treatment/add',
                    edit_url: 'order/treatment/edit',
                    del_url: 'order/treatment/del',
                    multi_url: 'order/treatment/multi',
                    import_url: 'order/treatment/import',
                    table: 'wanlshop_order_goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                Search:true,
                commonSearch:true,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false,visible:false},

                        // {field: 'order_id', title: __('Order_id')},
                        // {field: 'goods_id', title: __('Goods_id')},
                        // {field: 'shop_id', title: __('Shop_id')},
                        {field: 'order.order_no', title: __('Order.order_no'), operate: 'LIKE'},
                       // {field: 'shop_name', title: __('Shop_name'), operate: 'LIKE'},
                        {field: 'payway.pay_way_id', title: __('payway.pay_way_id'), searchList: {"6":__('payway.pay_way_id 6'),
                                "7":__('payway.pay_way_id 7'),"8":__('payway.pay_way_id 8'),"9":__('payway.pay_way_id 9'),"10":__('payway.pay_way_id 10'),
                                "11":__('payway.pay_way_id 11'),"12":__('payway.pay_way_id 12')},operate: '=', formatter: Table.api.formatter.normal},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'nickname', title: __('客户名'),operate: false,},
                        {field: 'staffs', title: __('销售人员'),operate: false,},
                        {field: 'servers', title: __('解痛师'),operate: false,},
                        // {field: 'difference', title: '规格', operate: false},
                        {field: 'number', title: __('Number'), operate: false},
                        {field: 'actual_payment', title: __('Actual_payment'), operate: false},
                        {field: 'discount_price', title: __('Discount_price'), operate: false},
                        {field: 'shop.id', title: '下单门店',searchList:$.getJSON('Ajax/getshoplist?type=arr'), visible:false},
                        {field: 'shop.shopname', title: '下单门店', operate: false},
                       // {field: 'freight_price', title: __('Freight_price'), operate: false},

                        // {field: 'goods_sku_id', title: __('Goods_sku_id')},
                        // {field: 'difference', title: __('Difference'), operate: 'LIKE'},
                       // {field: 'price', title: __('Price'), operate:'BETWEEN'},
                       
                        //{field: 'refund_id', title: __('Refund_id')},
                       // {field: 'refund_status', title: __('Refund_status'), searchList: {"0":__('Refund_status 0'),"1":__('Refund_status 1'),"2":__('Refund_status 2'),"3":__('Refund_status 3'),"4":__('Refund_status 4'),"5":__('Refund_status 5')}, formatter: Table.api.formatter.status},
//{field: 'market_price', title: __('Market_price'), operate: false},
                        {field: 'order.state', title: __('Order.state'), searchList: {"1":__('State 1'),"2":__('State 2'),"3":__('State 3'),"4":__('State 4'),"6":__('State 6'),"7":__('State 7'),"8":__('State 8'),"9":__('State 9'),"10":__('State 10'),"11":__('State 11'),"12":__('State 12'),"13":__('State 13')}, formatter: Table.api.formatter.status},
                       // {field: 'order.express_name', title: __('Order.express_name'), operate: false},
//{field: 'order.express_no', title: __('Order.express_no'), operate: false},
                        
                        // {field: 'order.dealtime', title: __('核销时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order.createtime', title: __('Order.createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order.paymenttime', title: __('Order.paymenttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                       
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        // {field: 'order.id', title: __('Order.id')},
                        // {field: 'order.user_id', title: __('Order.user_id')},
                        // {field: 'order.shop_id', title: __('Order.shop_id')},
                        // {field: 'order.order_no', title: __('Order.order_no'), operate: 'LIKE'},
                        // {field: 'order.address_id', title: __('Order.address_id')},
                        // {field: 'order.coupon_id', title: __('Order.coupon_id')},
                        // {field: 'order.isaddress', title: __('Order.isaddress')},
                        // {field: 'order.freight_type', title: __('Order.freight_type')},
                        // {field: 'order.express_name', title: __('Order.express_name'), operate: 'LIKE'},
                        // {field: 'order.express_no', title: __('Order.express_no'), operate: 'LIKE'},
                        // {field: 'order.state', title: __('Order.state')},
                        // {field: 'order.remarks', title: __('Order.remarks'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'order.createtime', title: __('Order.createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        
                        // {field: 'order.delivertime', title: __('Order.delivertime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'order.taketime', title: __('Order.taketime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'order.dealtime', title: __('Order.dealtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'order.updatetime', title: __('Order.updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'order.deletetime', title: __('Order.deletetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'order.status', title: __('Order.status'), formatter: Table.api.formatter.status},
                         {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            
                            buttons: [
                                {
                                    name: 'detail',
                                    text: '查看/编辑',
                                    title: '查看/编辑',
                                    icon: '',
                                    extend: 'data-area=\'["90%", "90%"]\'',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: "order/treatment/detail",
                                }],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'order/treatment/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'order/treatment/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'order/treatment/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        detail: function () {
            $(document).on("click", ".deliver-edit", function () {
                var deliver_id = $(this).data('ids');
                Fast.api.open('order/treatment/deliver/ids/' + deliver_id,'发货',{area: ['60%', '80%'],
                    callback:function(e){
                        location.reload();
                        parent.location.reload();
                       //console.log("123");
                      // window.parent.location.reload();
                    }
                     
                });
               
            });
            $(document).on("click", ".address-edit", function () {
                var deliver_id = $(this).data('ids');
                Fast.api.open('order/treatment/address/ids/' + deliver_id,'修改发货地址',{area: ['60%', '80%'],
                    callback:function(e){
                        location.reload();
                        parent.location.reload();
                       //console.log("123");
                      // window.parent.location.reload();
                    }
                     
                });
               
            });
            Controller.api.bindevent();
              
           
            
        },
        address: function () {
            $(document).on("click", ".success", function (e) {
                e.preventDefault();
                var ids = $(this).data('ids');
                var formData = $('#edit-form').serializeArray();
                    Fast.api.ajax({
                        url:'order/treatment/address/ids/'+ids,
                        data: formData
                    }, function (data) {
                        Toastr.success("操作成功");
                        window.parent.location.reload();
                        return false;
                    }, function () {
                        Toastr.error("操作失败");
                        return false;
                    });
               
            });
            Controller.api.bindevent();
            
            
        },
        deliver: function () {
            $(document).on("click", ".success", function (e) {
                e.preventDefault();
                var ids = $(this).data('ids');
                var formData = $('#edit-form').serializeArray();
                    Fast.api.ajax({
                        url:'order/treatment/deliver/ids/'+ids,
                        data: formData
                    }, function (data) {
                        Toastr.success("操作成功");
                        window.parent.location.reload();
                        return false;
                    }, function () {
                        Toastr.error("操作失败");
                        return false;
                    });
               
            });
            Controller.api.bindevent();
            
            
        },
        
        

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
